package cn.home.bootstrap.p2p;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 单个生产者对对个消费者，生产者
 */
public class Send {
  private final static String IP = "127.0.0.1";
  private final static int  PORT = 5672;
  private final static String USER_NAME = "donaldhan";
  private final static String PASSWORD = "123456";
  private final static String QUEUE_NAME = "hello";
  private final static Logger log = LoggerFactory.getLogger(Send.class);

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(IP);
    factory.setPort(PORT);
   /* factory.setUsername(USER_NAME);
    factory.setPassword(PASSWORD);*/
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    /*
     * @param queue the name of the queue
     * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
     * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
     * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
     * @param arguments other properties (construction arguments) for the queue
     */
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    /*
     * @param exchange the exchange to publish the message to
     * @param routingKey the routing key
     * @param props other properties for the message - routing headers etc
     * @param body the message body
     */
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    log.info(" Producer Sent the message to Queue({})，  '{}'",QUEUE_NAME, message);
    /*
    channel.close();
    connection.close();
    */
  }
}
